package com.beadwallet.alarm.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.beadwallet.alarm.entity.alarmInfo.AlarmInfo;
import com.beadwallet.alarm.service.alarm.AlarmInfoService;
import com.beadwallet.common.aspect.WebLog;
import com.beadwallet.common.controller.BaseController;
import com.beadwallet.common.enums.CodeEnum;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author master
 * @since 2017-12-12
 */
@Controller
@RequestMapping("/beadwallet/alarm/alarmInfo")
@Api(value = "alarm", description = "警报系统", tags = "alarm")
public class AlarmInfoController extends BaseController {

    @Autowired
    private AlarmInfoService iAlarmInfoService;

    @WebLog(description = "分页查询")
    @PostMapping("/pageQuery")
    @ResponseBody
    public String pageQuery(Integer pageId, Integer pageSize) {
        Wrapper<AlarmInfo> alarmInfo = new EntityWrapper<>();
        Page<AlarmInfo> page = new Page<>();
        int total = iAlarmInfoService.selectCount(alarmInfo);
        page.setSize(pageSize);
        page.setTotal(total);
        page.setCurrent(pageId);
        Page<AlarmInfo> alarmInfoPage = iAlarmInfoService.selectPage(page);
        return respJson(CodeEnum.SUCC, iAlarmInfoService.selectPage(page, alarmInfo));
    }

    @WebLog(description = "根据项目ID查询")
    @PostMapping("/queryByProjectId")
    @ResponseBody
    public String QueryByProjectId(String projectId) {
        Wrapper<AlarmInfo> alarmInfo = new EntityWrapper<>();
        alarmInfo.eq("project_id", projectId);
        return respJson(CodeEnum.SUCC, iAlarmInfoService.selectList(alarmInfo));
    }

    @WebLog(description = "添加")
    @PostMapping("/insertAlarmInfo")
    @ResponseBody
    public String insertAlarmInfo(Integer id, String projectId, String mailRecipient, String wechatRecipient, String ipAddress, String sendLevel, String sendType, String theSender,String projectName) {
        AlarmInfo alarmInfo = new AlarmInfo();
        alarmInfo.setId(id);
        alarmInfo.setProjectId(projectId);
        alarmInfo.setMailRecipient(mailRecipient);
        alarmInfo.setWechatRecipient(wechatRecipient);
        alarmInfo.setIpAddress(ipAddress);
        alarmInfo.setSendLevel(sendLevel);
        alarmInfo.setSendType(sendType);
        alarmInfo.setTheSender(theSender);
        alarmInfo.setProjectName(projectName);
        Wrapper<AlarmInfo> wrapper = new EntityWrapper<>();
        wrapper.eq("project_id",projectId);
        AlarmInfo alarmInfo1 = iAlarmInfoService.selectOne(wrapper);
        if(alarmInfo1==null){
            boolean b = iAlarmInfoService.insert(alarmInfo);
            if (b) {
                return respJson(CodeEnum.SUCC);
            } else {
                return respJson(CodeEnum.FAILURE);
            }
        }else{
            return respJson(CodeEnum.FAILURE,"project_id已存在，请重新输入");
        }
    }
    @WebLog(description = "修改")
    @PostMapping("/updateAlarmInfo")
    @ResponseBody
    public String updateAlarmInfo(Integer id,String projectId, String mailRecipient, String wechatRecipient, String ipAddress, String sendLevel, String sendType, String theSender,String projectName){
        AlarmInfo alarmInfo = iAlarmInfoService.selectById(id);
        alarmInfo.setProjectId(projectId);
        alarmInfo.setMailRecipient(mailRecipient);
        alarmInfo.setWechatRecipient(wechatRecipient);
        alarmInfo.setIpAddress(ipAddress);
        alarmInfo.setSendLevel(sendLevel);
        alarmInfo.setSendType(sendType);
        alarmInfo.setTheSender(theSender);
        alarmInfo.setProjectName(projectName);
        boolean b = iAlarmInfoService.updateById(alarmInfo);
        if (b) {
            return respJson(CodeEnum.SUCC);
        } else {
            return respJson(CodeEnum.FAILURE);
        }
    }
}
